home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / GRAPHICS.SWG / 0147_X-Mode Full Information.pas < prev    next >
Pascal/Delphi Source File  |  1995-03-03  |  15KB  |  382 lines

  1. (*
  2. Here is a great text for a start:
  3. Title:  INTRODUCTION TO MODE X
  4.  
  5. Version:        1.7
  6.  
  7. Author:  Robert Schmidt
  8.  
  9. Copyright: (C) 1993 of Ztiff Zox Softwear - refer to Status below.
  10.  
  11. Last revision:  28-Aug-93
  12.  
  13. Figures: 1. M13ORG - memory organization in mode 13h
  14.   2. MXORG - memory organization in unchained modes
  15.  
  16.   The figures are available both as 640x480x16 bitmaps
  17.   (in GIF format), and as 7-bit ASCII text (ASC) files.
  18.  
  19. C sources: 1. LIB.C v1.1 - simple graphics library for planar,
  20.                    256-color modes - optionally self-testing.
  21.  
  22.                 Excerpts from the source(s) appear in this article.
  23.                 Whenever there are conflicts, the external source file(s),
  24.   _not_ the excerpts provided here, are correct (or, at
  25.   least, newest).
  26.  
  27. Status:  This article, its associated figures and source listings
  28.   named above, are all donated to the public domain.
  29.   Do with it whatever you like, just don't claim it's your
  30.   work, or make money on it without doing some work
  31.                 yourself.  Please distribute the archive in its entirety.
  32.  
  33.   The standard disclaimer applies.
  34.  
  35. Index:  0. ABSTRACT
  36.   1. INTRODUCTION TO THE VGA AND ITS 256-COLOR MODE
  37.   2. GETTING MORE PAGES AND PUTTING YOUR FIRST PIXEL
  38.   3. THE ROAD FROM HERE
  39.   4. BOOKS ON THE SUBJECT
  40.                 5. BYE - FOR NOW
  41.  
  42.  
  43. 0. ABSTRACT
  44.  
  45. This text gives a fairly basic, yet technical, explanation to what, why
  46. and how Mode X is.  It first tries to explain the layout of the VGA
  47. memory and the shortcomings of the standard 320x200 256-color mode,
  48. then gives instructions on how one can progress from mode 13h to a
  49. multipage, planar 320x200 256-color mode, and from there to the
  50. quasi-standard 320x240 mode, known as Mode X.
  51.  
  52. A little experience in programming the standard VGA mode 13h
  53. (320x200 in 256 colors) is assumed.  Likewise a good understanding of
  54. hexadecimal notation and the concepts of segments and I/O ports is
  55. assumed.  Keep a VGA reference handy, which at least should have
  56. definitions of the VGA registers at bit level.
  57.  
  58. Throughout the article, a simple graphics library for unchained (planar)
  59. 256-color modes is developed.  The library supports the 320x200 and
  60. 320x240 modes, active and visible pages, and writing and reading
  61. individual pixels.
  62.  
  63.  
  64. 1. INTRODUCTION TO THE VGA AND ITS 256-COLOR MODE
  65.  
  66. Since its first appearance on the motherboards of the IBM PS/2 50, 60
  67. and 80 models in 1987, the Video Graphics Array has been the de facto
  68. standard piece of graphics hardware for IBM and compatible personal
  69. computers.  The abbreviation, VGA, was to most people synonymous with
  70. acceptable resolution (640x480 pixels), and a stunning rainbow of colors
  71. (256 from a palette of 262,144), at least compared to the rather gory
  72. CGA and EGA cards.
  73.  
  74. Sadly, to use 256 colors, the VGA BIOS limited the users to 320x200
  75. pixels, i.e. the well-known mode 13h.  This mode has one good and one
  76. bad asset.  The good one is that each pixel is easily addressable in
  77. the video memory segment at 0A000h.  Simply calculate the offset using
  78. this formula:
  79.  
  80. offset = (y * 320) + x;
  81.  
  82. Set the byte at this address (0A000h:offset) to the color you want, and
  83. the pixel is there.  Reading a pixel is just as simple: just read a
  84. byte.  This was heaven, compared to the hell of planes and masking
  85. registers needed in 16-color modes.  Suddenly, the distance from a
  86. graphics algorithm on paper to an implemented graphics routine was cut
  87. down to a fraction.  The results were impressively fast too!
  88.  
  89. The bad asset is that mode 13h is also limited to only one page, i.e.
  90. the VGA can only hold one screenful at any one time.  Most 16-color
  91. modes let the VGA hold more than one page, and this enables you to show
  92. one of the pages to the user, while drawing on another page in the
  93. meantime.  Page flipping is an important concept in making flicker free
  94. animations.  Nice looking and smooth scrolling is also almost impossible
  95. in this mode using plain VGA hardware.
  96.  
  97. Now, the alert reader might say: "Hold on a minute!  If mode 13h enables
  98. only one page, this means that there is memory for only one page.  But I
  99. know for a fact that all VGAs have at least 256 Kb RAM, and one 320x200
  100. 256-color page should consume only 320*200=64000 bytes, which is less
  101. than 64 Kb.  A standard VGA should room a little more than four 320x200
  102. pages!"  Quite correct, and to see how the BIOS puts this limitation on
  103. mode 13h, I'll elaborate a little on the memory organization of the VGA.
  104.  
  105. The memory is separated into four bit planes.  The reason for this stems
  106. from the EGA, where graphics modes were 16-color.  Using bit planes, the
  107. designers chose to let each pixel on screen be addressable by a single
  108. bit in a single byte in the video segment.  Assuming the palette has
  109. not been modified from the default, each plane represent one of the EGA
  110. primary colors: red, green, blue and intensity.  When modifying the bit
  111. representing a pixel, the Write Plane Enable register is set to the
  112. wanted color.  Reading is more complex and slower, since you can
  113. only read from a single plane at a time, by setting the Read Plane
  114. Select register.  Now, since each address in the video segment can
  115. access 8 pixels, and there are 64 Kb addresses, 8 * 65,536 = 524,288
  116. 16-color pixels can be accessed.  In a 320x200 16-color mode, this makes
  117. for about 8 (524,288/(320*200)) pages, in 640x480 you get nearly 2
  118. (524,288/(640*480)) pages.
  119.  
  120. In a 256-color mode, the picture changes subtly.  The designers decided
  121. to fix the number of bit planes to 4, so extending the logic above to 8
  122. planes and 256 colors does not work.  Instead, one of their goals was to
  123. make the 256-color mode as easily accessible as possible.  Comparing the
  124. 8 pixels/address in 16-color modes to the 1-to-1 correspondence of
  125. pixels and addresses of mode 13h, one can say that they have
  126. succeeded, but at a certain cost.  For reasons I am not aware of, the
  127. designers came up with the following effective, but memory-wasting
  128. scheme:
  129.  
  130. The address space of mode 13h is divided evenly across the four bit
  131. planes.  When an 8-bit color value is written to a 16-bit address in the
  132. VGA segment, a bit plane is automatically selected by the 2 least
  133. significant bits of the address.  Then all 8 bits of the data is written
  134. to the byte at the 16-bit address in the selected bitplane (have a look at
  135. figure 1).  Reading works exactly the same way.  Since the bit planes are so
  136. closely tied to the address, only every fourth byte in the video memory is
  137. accessible, and 192 Kb of a 256 Kb VGA go to waste.  Eliminating the
  138. need to bother about planes sure is convenientand beneficial, but in
  139. most people's opinion the loss of 3/4 of VGA memory is too much.
  140.  
  141. To accomodate this new method of accessing video memory, the VGA
  142. designers introduced a new configuration bit called Chain-4, which
  143. resides as bit number 3 in index 4 of the Sequencer.  In 16-color modes,
  144. the default state for this bit is off (zero), and the VGA operates as
  145. described earlier.  In the VGA's standard 256-color mode, mode 13h, this
  146. bit is turned on (set to one), and this turns the tieing of bit
  147. planes and memory address on.
  148.  
  149. In this state, the bit planes are said to be chained together.
  150.  
  151. Note that Chain-4 in itself is not enough to set a 256-color mode -
  152. there are other registers which deals with the other subtle changes in
  153. nature from 16 to 256 colors.  But, as we now will base our work with
  154. mode X on mode 13h, which already is 256-color, we won't bother about
  155. these for now.
  156.  
  157.  
  158. 2. GETTING MORE PAGES AND PUTTING YOUR FIRST PIXEL
  159.  
  160. The observant reader might at this time suggest that clearing the
  161. Chain-4 bit after setting mode 13h will give us access to all 256 Kb of
  162. video memory, as the two least significant bits of the byte address
  163. won't be `wasted' on selecting a bit plane.  This is correct.  You might
  164. also start feeling a little uneasy, because something tells you that
  165. you'll instantly loose the simple addressing of mode 13h.  Sadly, that
  166. is also correct.
  167.  
  168. At the moment Chain-4 is cleared, each byte offset addresses *four*
  169. sequential pixels.  Before writing to a byte offset in the video
  170. segment, you should make sure that the 4-bit mask in the Write Plane
  171. Enable register is set correctly, according to which of the four
  172. addressable pixels you want to modify.  In essence, it works like a
  173. 16-color mode with a twist.  See figure 2.
  174.  
  175. So, is this mode X?  Not quite.  We need to elaborate to the VGA how to
  176. fetch data for refreshing the monitor image.  Explaining the logic
  177. behind this is beyond the scope of this getting-you-started text, and it
  178. wouldn't be very interesting anyway.  Here is the minimum snippet of
  179. code to initiate the 4 page variant of mode 13h, written in plain C,
  180. using some DOS specific features (see header for a note about the
  181. sources included):
  182.  
  183. ----8<-------cut begin------
  184.  
  185. /* width and height should specify the mode dimensions.  widthBytes
  186.    specify the width of a line in addressable bytes. */
  187.  
  188. int width, height, widthBytes;
  189.  
  190. /* actStart specifies the start of the page being accessed by
  191.    drawing operations.  visStart specifies the contents of the Screen
  192.    Start register, i.e. the start of the visible page */
  193.  
  194. unsigned actStart, visStart;
  195.  
  196. /*
  197.  * set320x200x256_X()
  198.  * sets mode 13h, then turns it into an unchained (planar), 4-page
  199.  * 320x200x256 mode.
  200.  */
  201.  
  202. set320x200x256_X()
  203.  {
  204.  
  205.  union REGS r;
  206.  
  207.  /* Set VGA BIOS mode 13h: */
  208.  
  209.  r.x.ax = 0x0013;
  210.  int86(0x10, &r, &r);
  211.  
  212.  /* Turn off the Chain-4 bit (bit 3 at index 4, port 0x3c4): */
  213.  
  214.  outport(SEQU_ADDR, 0x0604);
  215.  
  216.  /* Turn off word mode, by setting the Mode Control register
  217.     of the CRT Controller (index 0x17, port 0x3d4): */
  218.  
  219.  outport(CRTC_ADDR, 0xE317);
  220.  
  221.  /* Turn off doubleword mode, by setting the Underline Location
  222.     register (index 0x14, port 0x3d4): */
  223.  
  224.  outport(CRTC_ADDR, 0x0014);
  225.  
  226.  /* Clear entire video memory, by selecting all four planes, then
  227.     writing 0 to the entire segment. */
  228.  
  229.  outport(SEQU_ADDR, 0x0F02);
  230.  memset(vga+1, 0, 0xffff); /* stupid size_t exactly 1 too small */
  231.  vga[0] = 0;
  232.  
  233.  /* Update the global variables to reflect dimensions of this
  234.     mode.  This is needed by most future drawing operations. */
  235.  
  236.         width   = 320;
  237.  height = 200;
  238.  
  239.         /* Each byte addresses four pixels, so the width of a scan line
  240.            in *bytes* is one fourth of the number of pixels on a line. */
  241.  
  242.         widthBytes = width / 4;
  243.  
  244.         /* By default we want screen refreshing and drawing operations
  245.            to be based at offset 0 in the video segment. */
  246.  
  247.  actStart = visStart = 0;
  248.  
  249.  }
  250.  
  251. ----8<-------cut end------
  252.  
  253. As you can see, I've already provided some of the mechanics needed to
  254. support multiple pages, by providing the actStart and visStart variables.
  255. Selecting pages can be done in one of two contexts:
  256.  
  257.  1) selecting the visible page, i.e. which page is visible on
  258.     screen, and
  259.  
  260.  2) selecting the active page, i.e. which page is accessed by
  261.     drawing operations
  262.  
  263. Selecting the active page is just a matter of offsetting our graphics
  264. operations by the address of the start of the page, as demonstrated in
  265. the put pixel routine below.  Selecting the visual page must be passed
  266. in to the VGA, by setting the Screen Start register.  Sadly enough, the
  267. resolution of this register is limited to one addressable byte, which
  268. means four pixels in unchained 256-color modes.  Some trickery is needed
  269. for 1-pixel smooth, horizontal scrolling, but I'll make that a subject
  270. for later.  The setXXXStart() functions provided here accept byte
  271. offsets as parameters, so they'll work in any mode.  If widthBytes and
  272. height are set correctly, so will the setXXXPage() functions.
  273.  
  274. ----8<-------cut begin------
  275.  
  276. /*
  277.  * setActiveStart() tells our graphics operations which address in video
  278.  * memory should be considered the top left corner.
  279.  */
  280.  
  281. setActiveStart(unsigned offset)
  282.  {
  283.  actStart = offset;
  284.  }
  285.  
  286. /*
  287.  * setVisibleStart() tells the VGA from which byte to fetch the first
  288.  * pixel when starting refresh at the top of the screen.  This version
  289.  * won't look very well in time critical situations (games for
  290.  * instance) as the register outputs are not synchronized with the
  291.  * screen refresh.  This refresh might start when the high byte is
  292.  * set, but before the low byte is set, which produces a bad flicker.
  293.  */
  294.  
  295. setVisibleStart(unsigned offset)
  296.  {
  297.  visStart = offset;
  298.  outport(CRTC_ADDR, 0x0C);  /* set high byte */
  299.  outport(CRTC_ADDR+1, visStart >> 8);
  300.  outport(CRTC_ADDR, 0x0D);  /* set low byte */
  301.  outport(CRTC_ADDR+1, visStart & 0xff);
  302.  }
  303.  
  304. /*
  305.  * setXXXPage() sets the specified page by multiplying the page number
  306.  * with the size of one page at the current resolution, then handing the
  307.  * resulting offset value over to the corresponding setXXXStart()
  308.  * function.  The first page number is 0.
  309.  */
  310.  
  311. setActivePage(int page)
  312.  {
  313.  setActiveStart(page * widthBytes * height);
  314.  }
  315.  
  316. setVisiblePage(int page)
  317.  {
  318.  setVisibleStart(page * widthBytes * height);
  319.  }
  320.  
  321. ----8<-------cut end------
  322.  
  323. Due to the use of bit planes, the graphics routines tend to get more
  324. complex than in mode 13h, and your first versions will generally tend to
  325. be a little slower than mode 13h algorithms.  Here's a put pixel routine
  326. for any unchained 256-color mode (it assumes that the 'width' variable
  327. from the above code is set correctly).  Optimizing is left as an exercise
  328. to you, the reader.  This will be the only drawing operation I'll cover
  329. in this article.
  330.  
  331. ----8<-------cut begin------
  332.  
  333. putPixel_X(int x, int y, char color)
  334.  {
  335.  
  336.  /* Each address accesses four neighboring pixels, so set
  337.     Write Plane Enable according to which pixel we want
  338.     to modify.  The plane is determined by the two least
  339.     significant bits of the x-coordinate: */
  340.  
  341.  outportb(0x3c4, 0x02);
  342.  outportb(0x3c5, 0x01 << (x & 3));
  343.  
  344.  /* The offset of the pixel into the video segment is
  345.     offset = (width * y + x) / 4, and write the given
  346.     color to the plane we selected above.  Heed the active
  347.     page start selection. */
  348.  
  349.  vga[(unsigned)(widthBytes * y) + (x / 4) + actStart] = color;
  350.  
  351.  }
  352.  
  353. char getPixel_X(int x, int y)
  354.  {
  355.  
  356.  /* Select the plane from which we must read the pixel color: */
  357.  
  358.  outport(GRAC_ADDR, 0x04);
  359.  outport(GRAC_ADDR+1, x & 3);
  360.  
  361.  return vga[(unsigned)(widthBytes * y) + (x / 4) + actStart];
  362.  
  363.  }
  364.  
  365. ----8<-------cut end------
  366.  
  367.  
  368. However, by now you should be aware of that the Write Plane Enable
  369. register isn't limited to selecting just one bit plane, like the
  370. ReadPlane Select register is.  You can enable any combination of all
  371. four to be written.  This ability to access 4 pixels with one
  372. instruction helps quadrupling the speed, especially when drawing
  373. horizontal lines and filling polygons of a constant color.  Also, most
  374. block algorithms can be optimized in various ways so that they need only
  375. a constant number of OUTs (typically four) to the Write Plane Enable
  376. register.  OUT is a relatively slow instruction.
  377.  
  378. The gained ability to access the full 256 Kb of memory on a standard
  379. VGA enables you to do paging and all the goodies following from that:
  380. smooth scrolling over large maps, page flipping for flicker free
  381. animation... and I'll leave something for your own imagination.
  382. *)